package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author xuxu
 * @Date 2020/4/14
 * @Version v1.0
 * @Description 菜单项控制器
 */
@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;

    //新增菜单
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping("add")
    public Result add (@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConst.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_MENU_FAIL);
        }
    }

    //编辑菜单
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        log.debug("menu ==={}" + menu);
        try {
            //调用service中的编辑方法
            menuService.edit(menu);
            //编辑成功, 返回编辑成功的信息
            return new Result(true, MessageConst.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑失败, 返回编辑失败的信息
            return new Result(false, MessageConst.EDIT_MENU_FAIL);
        }
    }

    //通过id,查询
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    //删除
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            //调用service中的删除方法
            menuService.deleteById(id);
            //删除成功, 返回删除成功的信息
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "该菜单被占用, 不能被删除");
        }
    }

    //查询菜单
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Menu> menuList = menuService.findAll();
            System.out.println("menuList = " + menuList);
            return new Result(true, MessageConst.ACTION_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //日志打印
//        log.debug("queryPageBean ===currentPage:{}===pageSize:{}====queryString:{}", queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

        try {
            //调用Service获取分页结果数据
            PageResult pageResult = menuService.pageQuery(queryPageBean);
            //成功，直接返回有内容的结果
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //失败，返回初始化结果，记录为0，数据为空
            return new PageResult(0L,null);
        }
    }
}
